package com.cafuc.javaEE.controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(value = {"/test/helloWorld"})
public class HelloWorldServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("==== Call get method =====");
        resp.setContentType("text/html; charset = UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.write("<h1>hello world! 大勋花</h1>");
        //参数取值
        pw.write(String.format("RemoteAddr:%s, RemoteHost:%s, RemotePort:%s <br>",
                req.getRemoteAddr(), req.getRemoteHost(), req.getRemotePort()));
        pw.write(String.format("LocalAddr: %s, LocalName: %s, LocalPort: %s <br>",
                req.getLocalAddr(), req.getLocalName(), req.getLocalPort()));
        pw.write(String.format("RequestURL: %s, RequestURI: %s, ContextPath: %s <br>",
                req.getRequestURL(), req.getRequestURI(), req.getContextPath()));
        pw.write(String.format("ServerName: %s, ServerPort: %s, ServletPath: %s <br>",
                req.getServerName(), req.getServerPort(), req.getServletPath()));
        pw.write(String.format("ServerName: %s",
                req.getParameter("name")));
        pw.flush();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("==== Call post method =====");
        super.doPost(req, resp);
    }

    @Override
    public void init() throws ServletException {
        System.out.println("==== Call init method =====");
        super.init();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        System.out.println("==== Call service method =====");
        super.service(req, resp);
    }

    @Override
    public void destroy() {
        System.out.println("==== Call destroy method =====");
        super.destroy();
    }
}
